<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :async="true"
            :title="popupTitle"
            width="1000px"
            @close="handleClose"
            @confirm="handleSubmit"
        >
            <el-form ref="formRef" :model="formData" :rules="formRules" label-width="90px">
                <el-form-item label="账户名" prop="account">
                    <el-input v-model="formData.account" clearable placeholder="请输入账户名" />
                </el-form-item>
                <el-form-item label="头像" prop="avatar">
                    <el-input v-model="formData.avatar" clearable placeholder="请输入头像" />
                </el-form-item>
                <el-form-item label="名字" prop="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入名字" />
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="formData.password" clearable placeholder="请输入密码" />
                </el-form-item>
                <el-form-item label="密码盐" prop="salt">
                    <el-input v-model="formData.salt" clearable placeholder="请输入密码盐" />
                </el-form-item>
                <el-form-item label="所属经销商ID" prop="dealer_id">
                    <el-input v-model="formData.dealer_id" clearable placeholder="请输入所属经销商ID" />
                </el-form-item>
                <el-form-item label="手机号" prop="mobile">
                    <el-input v-model="formData.mobile" clearable placeholder="请输入手机号" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" />
                </el-form-item>
                <el-form-item label="是否禁用" prop="is_disable">
                    <el-input v-model="formData.is_disable" clearable placeholder="请输入是否禁用" />
                </el-form-item>
                <el-form-item label="分账比例" prop="dealer_share_ratio">
                    <el-input v-model="formData.dealer_share_ratio" clearable placeholder="请输入分账比例" />
                </el-form-item>
                <el-form-item label="登录时间" prop="login_time">
                    <el-input v-model="formData.login_time" clearable placeholder="请输入登录时间" />
                </el-form-item>
                <el-form-item label="登录IP" prop="login_ip">
                    <el-input v-model="formData.login_ip" clearable placeholder="请输入登录IP" />
                </el-form-item>
                <el-form-item label="账户ID" prop="account_id">
                    <el-input v-model="formData.account_id" clearable placeholder="请输入账户ID" />
                </el-form-item>
                <el-form-item label="通知剩余次数" prop="notice_num">
                    <el-input v-model="formData.notice_num" clearable placeholder="请输入通知剩余次数" />
                </el-form-item>
                <el-form-item label="余额" prop="money">
                    <el-input v-model="formData.money" clearable placeholder="请输入余额" />
                </el-form-item>
                <el-form-item label="冻结余额" prop="frozen_money">
                    <el-input v-model="formData.frozen_money" clearable placeholder="请输入冻结余额" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" name="boomAgentEdit" setup>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiBoomAgentDetail } from '@/api/agent'
import type { PropType } from 'vue'

defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑代理商表' : '新增代理商表'
})

// 表单数据
const formData = reactive({
    id: '',
    account: '',
    avatar: '',
    name: '',
    password: '',
    salt: '',
    dealer_id: '',
    mobile: '',
    remark: '',
    is_disable: '',
    dealer_share_ratio: '',
    login_time: '',
    login_ip: '',
    account_id: '',
    notice_num: '',
    money: '',
    frozen_money: ''
})


// 表单验证
const formRules = reactive<any>({
    account: [{
        required: true,
        message: '请输入账户名',
        trigger: ['blur']
    }],
    name: [{
        required: true,
        message: '请输入名字',
        trigger: ['blur']
    }],
    password: [{
        required: true,
        message: '请输入密码',
        trigger: ['blur']
    }],
    salt: [{
        required: true,
        message: '请输入密码盐',
        trigger: ['blur']
    }],
    dealer_id: [{
        required: true,
        message: '请输入所属经销商ID',
        trigger: ['blur']
    }],
    mobile: [{
        required: true,
        message: '请输入手机号',
        trigger: ['blur']
    }],
    is_disable: [{
        required: true,
        message: '请输入是否禁用',
        trigger: ['blur']
    }],
    notice_num: [{
        required: true,
        message: '请输入通知剩余次数',
        trigger: ['blur']
    }],
    money: [{
        required: true,
        message: '请输入余额',
        trigger: ['blur']
    }]
})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }


}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiBoomAgentDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    mode.value == 'edit'
        ? await apiBoomAgentEdit(data)
        : await apiBoomAgentAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}


defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
